package com.shanyue.redwine.activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.widget.ImageView;
import cn.pedant.SweetAlert.SweetAlertDialog;
import com.shanyue.redwine.BuildConfig;
import com.shanyue.redwine.R;
import com.shanyue.redwine.utils.AppTaskManager;
import com.shanyue.redwine.utils.Constants;
import com.shanyue.redwine.utils.SPUtils;
import com.shanyue.redwine.utils.SweetAlertDialogFactory;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;
import io.reactivex.functions.Consumer;


/**
 * Created by Administrator on 2015/10/21.
 */
public class ActivityLaunch extends ActivityBase {
    Consumer<Permission> mPermissionConsumer = new Consumer<Permission>() {
        @Override
        public void accept(Permission permission) {
            if (permission.granted) {//每一个授权 都检测是否全部授权 如果全部授权
                if (permission.name.equals(Manifest.permission.CAMERA) && mRxPermissions.isGranted(Manifest
                        .permission.CAMERA) && mRxPermissions.isGranted(Manifest.permission.CAMERA) && mRxPermissions
                        .isGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE) && mRxPermissions.isGranted(Manifest
                        .permission.CALL_PHONE) && mRxPermissions.isGranted(Manifest.permission.ACCESS_FINE_LOCATION)) {
                    Intent intent;
                    if (!SPUtils.contains(mActivity, Constants.SP_KEY_OLD_VERSION) || ((int) SPUtils.get(mActivity,
                            Constants.SP_KEY_OLD_VERSION, 0)) != BuildConfig.VERSION_CODE) {
                        intent = new Intent(getContext(), ActivityGuide.class);
                        startActivity(intent);
                        AppTaskManager.getAppManager().finishActivity(mActivity);
                    } else {
                        AppTaskManager.getAppManager().finishActivity(mActivity);
                    }
                }
            } else if (permission.shouldShowRequestPermissionRationale) {//拒绝权限 没有选中不在询问
                SweetAlertDialogFactory.build(mActivity, SweetAlertDialog.WARNING_TYPE, false).setContentText
                        ("请授权程序运行所需要的权限").setConfirmClickListener(dialog -> {
                    dialog.dismiss();
                    mRxPermissions.requestEach(permission.name).subscribe(mPermissionConsumer);
                }).show();
            } else {//拒绝权限 并选择不在询问
                SweetAlertDialogFactory.build(mActivity, SweetAlertDialog.WARNING_TYPE, false).setContentText
                        ("拒绝程序授权会导致程序无法正常运行.请在手机设置->应用程序->" + getString(R.string.app_name) + "->权限 进行手动授权")
                        .setConfirmClickListener(dialog -> {
                    dialog.dismiss();
                    AppTaskManager.getAppManager().AppExit(mActivity);
                }).show();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //        MineApplication.addActivity(this);
        requestWindowFeature(Window.FEATURE_NO_TITLE);// 隐藏标题
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        mRxPermissions = new RxPermissions(this);
        // 设置全屏
        ImageView imageView = new ImageView(this);
        imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup
                .LayoutParams.MATCH_PARENT));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setImageResource(R.mipmap.launch);
        setContentView(imageView);
        AnimationSet animationSet = new AnimationSet(true);
        AlphaAnimation alphaAnimation = new AlphaAnimation(0.3f, 1);
        animationSet.addAnimation(alphaAnimation);
        animationSet.setDuration(3000);
        animationSet.setFillAfter(true);
        animationSet.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animan) {
                Intent intent;
                if (!SPUtils.contains(mActivity, Constants.SP_KEY_OLD_VERSION) || ((int) SPUtils.get(mActivity,
                        Constants.SP_KEY_OLD_VERSION, 0)) != BuildConfig.VERSION_CODE) {
                    intent = new Intent(getContext(), ActivityGuide.class);
                    startActivity(intent);
                    AppTaskManager.getAppManager().finishActivity(mActivity);
                } else {

                    AppTaskManager.getAppManager().finishActivity(mActivity);
                }
            }
        });
        if (mRxPermissions.isGranted(Manifest.permission.CAMERA) && mRxPermissions.isGranted(Manifest.permission
                .WRITE_EXTERNAL_STORAGE) && mRxPermissions.isGranted(Manifest.permission.CALL_PHONE) &&
                mRxPermissions.isGranted(Manifest.permission.ACCESS_FINE_LOCATION)) {
            imageView.setAnimation(animationSet);
            imageView.startAnimation(animationSet);
        } else {
            mRxPermissions.requestEach(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.CALL_PHONE, Manifest.permission.ACCESS_FINE_LOCATION).subscribe
                    (mPermissionConsumer);
        }

    }


    Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppTaskManager.getAppManager().finishActivity(mActivity);
    }
}
